package com.fingard.dsp.bank.directbank.bocom15;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.bocom13.BOCOM13Header;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Calendar;
import java.util.Date;

public class BOCOM15Base extends DirectBase {
    protected String txCode = "";
    protected String reqSeqID;
    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }
    protected String getQryTransID() {
        Date tmpDateNow = new Date();
        return Format.DateTime17SSSFormat.format(tmpDateNow);
    }
    protected void writeCommonHeader(XmlTextWriter xmlWriter) throws Exception{
        if (StringHelper.isNullOrEmpty(txCode) || StringHelper.isNullOrEmpty(reqSeqID)) {
            throw new Exception("未赋值txCode或reqSeqID");
        }
        Date date = new Date();
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("TX");
        xmlWriter.writeStartElement("TX_HEADER");
        xmlWriter.writeElementString("SYS_HDR_LEN", "0", true);//<TX_HEADER>节点内容的长度（包括节点本身）
        xmlWriter.writeElementString("SYS_PKG_VRSN", "01");
        xmlWriter.writeElementString("SYS_TTL_LEN", "0", true);//从<?xml version="1.0" encoding="UTF-8" ?>到</TX>的长度
        xmlWriter.writeElementString("SYS_REQ_SEC_ID", getBankConfig().getValueAsString("remoteSecNode"));
        xmlWriter.writeElementString("SYS_SND_SEC_ID", getBankConfig().getValueAsString("localSecNode"));//服务版本号105005
        xmlWriter.writeElementString("SYS_TX_CODE", txCode);
        xmlWriter.writeElementString("SYS_TX_TYPE", "020000");
        xmlWriter.writeElementString("SYS_TX_VRSN", "01");
        xmlWriter.writeElementString("SYS_EVT_TRACE_ID", getQryTransID()+"00000001");
        xmlWriter.writeElementString("SYS_SND_SERIAL_NO", Format.DateAndHourFormat.format(date));
        xmlWriter.writeElementString("SYS_PKG_TYPE", "1");
        xmlWriter.writeElementString("SYS_MSG_LEN", "0", true);//<TX_BODY>节点内容的长度（包括节点本身）。
        xmlWriter.writeElementString("SYS_IS_ENCRYPTED", "3");
        xmlWriter.writeElementString("SYS_ENCRYPT_TYPE", "3");
        xmlWriter.writeElementString("SYS_COMPRESS_TYPE", "0");
        xmlWriter.writeElementString("SYS_EMB_MSG_LEN", "0");
        xmlWriter.writeElementString("SYS_REQ_TIME", Format.DateTime17SSSFormat.format(date));
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, 1);// 24小时制
        date = cal.getTime();
        xmlWriter.writeElementString("SYS_TIME_LEFT", Format.TimeSSFormat.format(date));
        xmlWriter.writeElementString("SYS_PKG_STS_TYPE", "00"); //发起方流水号
        xmlWriter.writeEndElement(); // TX_HEADER
        xmlWriter.writeStartElement("TX_BODY");
        xmlWriter.writeStartElement("ENTITY");
    }
    protected void writeCommonHeader(XmlTextWriter xmlWriter, BOCOM13Header tmpBankHeader) {
        xmlWriter.writeStartElement("Req");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("TrCode", tmpBankHeader.transCode);/* 交易码:参见《交通银行资金清算平台数据字典》2.1交易码 */
        xmlWriter.writeStartElement("Sender");/* 交易请求方 */
        xmlWriter.writeElementString("OrgType", "I");/* 机构类型 */
        xmlWriter.writeElementString("OrgId", tmpBankHeader.corpNo);/* 机构ID */
        xmlWriter.writeElementString("BrchId", "");/* 交易区域(保险分公司编码) */
        xmlWriter.writeElementString("SubBrchId", "");/* 交易机构 */
        xmlWriter.writeElementString("TermId", "");/* 交易终端号 */
        xmlWriter.writeElementString("TrOper", "");/* 交易柜员号(保险方柜员代码) */
        xmlWriter.writeElementString("TrOperName", "");/* 交易柜员名称(保险方柜员名称) */
        xmlWriter.writeElementString("ChanNo", "93");/* 交易渠道(固定填写"93") */
        xmlWriter.writeElementString("OriBusDate", "");/* 原业务日期 */
        xmlWriter.writeElementString("OriSeqNo", tmpBankHeader.reqSeqID);/* 原交易流水号 */
        xmlWriter.writeElementString("BusDate", Format.Date8Format.format(new Date()));/* 业务日期 */
        xmlWriter.writeElementString("SeqNo", tmpBankHeader.reqSeqID);/* 交易流水号 */
        xmlWriter.writeElementString("TrDate", Format.Date8Format.format(new Date()));/* 交易日期 */
        xmlWriter.writeElementString("TrTime", Format.Time6Format.format(new Date()));/* 交易时间 */
        xmlWriter.writeEndElement();
        xmlWriter.writeStartElement("Recver");/* 交易应答方 */
        xmlWriter.writeElementString("OrgType", "B");/* 机构类型 */
        xmlWriter.writeElementString("OrgId", "3012900");/* 机构ID(3012900:交通银行) */
        xmlWriter.writeElementString("BrchId", "");/* 交易区域(银行分行代码) */
        xmlWriter.writeElementString("SubBrchId", "");/* 交易机构(银行机构代码) */
        xmlWriter.writeEndElement();
        xmlWriter.writeEndElement();
    }
    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        String url = bankFront.serverURL+"/"+txCode;
        WriteBankLogLn("2.提交银行的地址：" + url);
        WriteBankLogStep2(p_reqStr);
        byte[] tmpToWriteBytes = p_reqStr.getBytes(getCharset());
        WebRequest tmpWebRequest = new WebRequest(url);
        tmpWebRequest.setConnectTimeout(600000);
        tmpWebRequest.setRequestProperty("Content-Type", "application/xml");
        tmpWebRequest.setRequestProperty("Connection", "keep-alive");
        retStr = tmpWebRequest.upload(tmpToWriteBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }
}
